package com.example.zsf.fragment;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.example.zsf.R;
import com.example.zsf.activity.account.LoginActivity;
import com.example.zsf.base.Presenter;
import com.example.zsf.base.Result;
import com.example.zsf.base.ZSFFragment;
import com.example.zsf.base.ZSFGlobal;
import com.example.zsf.presenter.AccountPresenter;
import com.jyn.vcview.VerificationCodeView;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class InputVerificationFragment extends ZSFFragment {


    @BindView(R.id.textView20)
    TextView title;
    @BindView(R.id.textView21)
    TextView remainTv;
    @BindView(R.id.verificationCodeView)
    VerificationCodeView verificationCodeView;

    private Unbinder unbinder;

    private LoginActivity.FetchVerificationCodeListener listener;
    private String validate;

    private String phoneNumber;
    private CountDownTimer countDownTimer;
    private boolean isNeedReEmit;


    public InputVerificationFragment(String phoneNumber, String validate, LoginActivity.FetchVerificationCodeListener listener) {
        this.phoneNumber = phoneNumber;
        this.validate = validate;
        this.listener = listener;
    }

    public InputVerificationFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_input_verification, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        initUI();
        initRemain();
        emitMessage(false);
        return inflate;
    }

    private void initUI() {
        title.setText("短信已经发送至 +86 " + phoneNumber);
        verificationCodeView.setOnCodeFinishListener(new VerificationCodeView.OnCodeFinishListener() {
            @Override
            public void onTextChange(View view, String content) {

            }

            @Override
            public void onComplete(View view, String content) {
                requestLogin(content);
            }
        });
    }

    private void initRemain() {
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                remainTv.setText(l / 1000 + "s 后可以重新获取");
            }

            @Override
            public void onFinish() {
                remainTv.setTextColor(getResources().getColor(R.color.colorTitleBlue));
                remainTv.setText("重新获取验证码");
                isNeedReEmit = true;
            }
        };
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        countDownTimer.cancel();
        countDownTimer = null;
    }

    @OnClick(R.id.textView21)
    public void onViewClicked() {
        if (isNeedReEmit) {
            emitMessage(true);
        }
    }


    private void emitMessage(boolean isNeedVerificationCode) {
        if (isNeedVerificationCode) {
            if (listener != null) {
                listener.fetchVerificationCode(phoneNumber);
            }
        } else {
            verifySuccess(validate);
        }
    }

    private void requestMessageCode() {
        disposed(AccountPresenter.sendMessage(phoneNumber, validate, new Presenter.PresenterListener<Result<HashMap>>() {
            @Override
            public void onSuccess(Result<HashMap> object) {
                if (!object.code.equals("1")) {
                    ToastUtils.showShort(object.msg);
                }
            }

            @Override
            public void onError(String error) {
                ToastUtils.showShort(error);
            }
        }));
    }

    private void requestLogin(String code) {
        disposed(AccountPresenter.login(phoneNumber, code, new Presenter.PresenterListener<Result<HashMap>>() {
            @Override
            public void onSuccess(Result<HashMap> object) {
                if (object.code.equals("1")) {
                    ZSFGlobal.login(String.valueOf(object.data.get("id")), (String) object.data.get("token"), phoneNumber);
                    getActivity().finish();
                } else {
                    ToastUtils.showShort(object.msg);
                }
            }

            @Override
            public void onError(String error) {
                ToastUtils.showShort(error);
            }
        }));
    }


    public void verifySuccess(String validate) {
        this.validate = validate;
        remainTv.setTextColor(getResources().getColor(R.color.color999));
        remainTv.setText("60s 后可以重新获取");
        isNeedReEmit = false;
        countDownTimer.start();

        requestMessageCode();
    }


}